local jixun = fk.CreateSkill{
  name = "xiaobai__jixun"
}

Fk:loadTranslationTable{
  ["xiaobai__jixun"] = "羁讯",
  [":xiaobai__jixun"] = "出牌阶段限一次，你可以获得一名其他角色两张牌，并令其此回合结束后收回这些牌，"..
    "然后其可以视为使用一张由你指定目标的【杀】（无视距离），否则其只能从弃牌堆收回之。",
  ["#xiaobai__jixun"] = "羁讯：获得 %dest 的两张牌",
  ["#xiaobai__jixun_useExtraSlash"] = "羁讯：你可以使用由 %dest 指定目标的【杀】",
  ["@$xiaobai__jixun_getCards-turn"] = "羁讯",
  ["#xiaobai__jixun_slashTarget"] = "羁讯：替 %dest 选择【杀】的目标",
  ["@@xiaobai__jixun_fromDiscard"] = "羁讯 弃牌堆"
}

jixun:addEffect("active", {
  can_use = function (self, player)
    return player:usedSkillTimes(jixun.name) < 1
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    return to_select ~= player and #selected < 1 and to_select:getHandcardNum() >= 2
  end,
  card_filter = Util.FalseFunc,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected > 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local cards = room:askToChooseCards(player, {
      min = 2,
      max = 2,
      flag = "he",
      skill_name = jixun.name,
      target = target,
      prompt = "#xiaobai__jixun::"..target.id 
    })
    room:obtainCard(player, cards, true, fk.ReasonPrey, player, jixun.name)
    for _, cid in ipairs(cards) do
      room:addTableMark(target, "@$xiaobai__jixun_getCards-turn", cid)
    end
    local slash = Fk:cloneCard("slash")
    slash.skillName = jixun.name
    room:setPlayerMark(player, "xiaobai__jixun_p", target.id)
    if target:canUse(slash, {bypass_distances = true, bypass_times = true}) and room:askToSkillInvoke(target, {
      skill_name = jixun.name,
      prompt = "#xiaobai__jixun_useExtraSlash::"..player.id
    }) then
      local did,dat = room:askToUseActiveSkill(player, {
        skill_name = "xiaobai__jixun_slashTarget",
        cancelable = false,
        prompt = "#xiaobai__jixun_slashTarget::"..target.id,
        extra_data = {
          skillName = jixun.name,
        }
      })
      room:setPlayerMark(player, "xiaobai__jixun_p", 0)
      if did and dat then
        room:useCard{
          card = slash,
          from = target,
          tos = dat.targets,
          extraUse = true, 
        }
      end
    else
      room:addPlayerMark(target, "@@xiaobai__jixun_fromDiscard")
    end
    
  end
})

jixun:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self, true, true) and target == player and table.find(player.room.alive_players, function (p)
      return #p:getTableMark("@$xiaobai__jixun_getCards-turn") > 0
    end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local targets = table.filter(player.room.alive_players, function (p)
      return #p:getTableMark("@$xiaobai__jixun_getCards-turn") > 0
    end)
    for _, p in ipairs(targets) do
      local obtains = {}
      if p:getMark("@@xiaobai__jixun_fromDiscard") > 0 then
        for _, cid in ipairs(p:getTableMark("@$xiaobai__jixun_getCards-turn")) do
          if player.room:getCardArea(cid) == Card.DiscardPile then
            table.insert(obtains, cid)
          end
        end
      else
        obtains = p:getTableMark("@$xiaobai__jixun_getCards-turn")
      end
      player.room:obtainCard(p, obtains, true, fk.ReasonPrey, player, jixun.name)
    end
  end
})

local ct = fk.CreateSkill{
  name = "xiaobai__jixun_slashTarget"
}

ct:addEffect("active", {
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards, c, extra_data)
    local card = Fk:cloneCard("slash")
    card.skillName = jixun.name
    return card.skill:targetFilter(Fk:currentRoom():getPlayerById(player:getMark("xiaobai__jixun_p")), to_select, selected, {}, card, {bypass_times = true, bypass_distances = true})
  end,
  feasible = function (self, player, selected, selected_cards, card)
    local card = Fk:cloneCard("slash")
    card.skillName = jixun.name
    return card.skill:feasible(Fk:currentRoom():getPlayerById(player:getMark("xiaobai__jixun_p")), selected, {}, card)
  end
})

return {ct, jixun}